$(function() {

	// jobGroup change, job list init and select
	$("#jobGroup").on("change", function () {
		var jobGroup = $(this).children('option:selected').val();
		$.ajax({
			type : 'POST',
			async: false,   // async, avoid js invoke pagelist before jobId data init
			url : base_url + '/joblog/getJobsByGroup',
			data : {"jobGroup":jobGroup},
			dataType : "json",
			success : function(data){
				if (data.code == 200) {
					$("#jobId").html( '<option value="0" >'+ I18n.system_all +'</option>' );
					$.each(data.content, function (n, value) {
						$("#jobId").append('<option value="' + value.id + '" >' + value.jobDesc + '</option>');
					});
					if ($("#jobId").attr("paramVal")){
						$("#jobId").find("option[value='" + $("#jobId").attr("paramVal") + "']").attr("selected",true);
					}
				} else {
					layer.open({
						title: I18n.system_tips ,
						btn: [ I18n.system_ok ],
						content: (data.msg || I18n.system_api_error ),
						icon: '2'
					});
				}
			},
		});
	});
	if ($("#jobGroup").attr("paramVal")){
		$("#jobGroup").find("option[value='" + $("#jobGroup").attr("paramVal") + "']").attr("selected",true);
		$("#jobGroup").change();
	}

	// init date tables
	var alarmRecordListTable = $("#alarm_record_list").dataTable({
		"deferRender": true,
		"processing" : true,
		"serverSide": true,
		"ajax": {
			url: base_url + "/alarm/record/page",
			type:"post",
			data : function ( d ) {
				var obj = {};
				obj.jobGroup = $('#jobGroup').val();
				obj.jobId = $('#jobId').val();
				obj.alarmType = $('#alarmType').val();
				obj.alarmStatus = $('#alarmStatus').val();
				obj.start = d.start;
				obj.length = d.length;
				return obj;
			}
		},
		"searching": false,
		"ordering": false,
		//"scrollX": true,	// scroll x，close self-adaption
		"columns": [
			{
				"data": 'id',
				"visible" : false,
				"width":'10%'
			},
			{
				"data": 'jobGroupName',
				"visible" : true,
				"width":'10%'
			},
			{
				"data": 'jobName',
				"visible" : true,
				"width":'10%'
			},
			{
				"data": 'jobHandler',
				"visible" : true,
				"width":'10%'
			},
			{
				"data": 'alarmType',
				"visible" : true,
				"width":'15%',
				"render": function ( data, type, row ) {
					if (data == 1) {
						return I18n.alarm_config_type_1
					}else if (data == 2) {
						return I18n.alarm_config_type_2
					}else if (data == 3) {
						return I18n.alarm_config_type_3
					}  else {
						return I18n.alarm_config_type_4
					}
				}
			},
			{
				"data": 'alarmStatus',
				"visible" : true,
				"width":'10%',
				"render": function ( data, type, row ) {
					if (data == '1') {
						return I18n.alarm_status_1
					} else if (data == '2') {
						return I18n.alarm_status_2
					}else {
						return I18n.alarm_status_3
					}
				}
			},
			{
				"data": 'errorMsg',
				"visible" : true,
				"width":'10%'
			},
			{
				"data": I18n.system_opt ,
				"width":'15%',
				"render": function ( data, type, row ) {
					return function(){
						// html
						tableData['key'+row.id] = row;
						var html = '<p id="'+ row.id +'" >'+
							'<button class="btn btn-warning btn-xs jobLog" type="button">'+ I18n.joblog_rolling_log +'</button>  '+
							'</p>';
						return html;
					};
				}
			}
		],
		"language" : {
			"sProcessing" : I18n.dataTable_sProcessing ,
			"sLengthMenu" : I18n.dataTable_sLengthMenu ,
			"sZeroRecords" : I18n.dataTable_sZeroRecords ,
			"sInfo" : I18n.dataTable_sInfo ,
			"sInfoEmpty" : I18n.dataTable_sInfoEmpty ,
			"sInfoFiltered" : I18n.dataTable_sInfoFiltered ,
			"sInfoPostFix" : "",
			"sSearch" : I18n.dataTable_sSearch ,
			"sUrl" : "",
			"sEmptyTable" : I18n.dataTable_sEmptyTable ,
			"sLoadingRecords" : I18n.dataTable_sLoadingRecords ,
			"sInfoThousands" : ",",
			"oPaginate" : {
				"sFirst" : I18n.dataTable_sFirst ,
				"sPrevious" : I18n.dataTable_sPrevious ,
				"sNext" : I18n.dataTable_sNext ,
				"sLast" : I18n.dataTable_sLast
			},
			"oAria" : {
				"sSortAscending" : I18n.dataTable_sSortAscending ,
				"sSortDescending" : I18n.dataTable_sSortDescending
			}
		}
	});

	// table data
	var tableData = {};

	// search btn
	$('#searchBtn').on('click', function(){
		alarmRecordListTable.fnDraw();
	});

	$("#alarm_record_list").on('click', '.jobLog',function() {
		var id = $(this).parent('p').attr("id");
		var row = tableData['key'+id];
		window.open(base_url + '/joblog/logDetailPage?id=' +row.logId, '_blank');
	})

});
